<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑教师</title>
    <link rel="stylesheet" href="../css/edit_teacher.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
    <h1>编辑教师</h1>
    <form id="editTeacherForm">
        <label for="teacherid">教师ID：</label>
        <input type="text" id="teacherid" name="teacherid" readonly>

        <label for="name">姓名：</label>
        <input type="text" id="name" name="name">

        <label for="gender">性别：</label>
        <select id="gender" name="gender">
            <option value="M">男</option>
            <option value="F">女</option>
        </select>

        <label for="phone">联系电话：</label>
        <input type="text" id="phone" name="phone">

        <label for="address">地址：</label>
        <input type="text" id="address" name="address">

        <label for="partymember">党员：</label>
        <input type="checkbox" id="partymember" name="partymember">

        <label for="position">职位：</label>
        <input type="text" id="position" name="position">

        <label for="departmentid">部门ID：</label>
        <input type="text" id="departmentid" name="departmentid">

        <button type="button" id="saveBtn">保存修改</button>
        <button type="button" onclick="window.location.href='teacher.html'">返回</button>
    </form>
</div>

<script>
    $(document).ready(function () {
        // 获取URL中的教师ID参数
        const urlParams = new URLSearchParams(window.location.search);
        const teacherId = urlParams.get("id");
        console.log("teacherid-->" + teacherId);
        // 查询并加载教师信息
        if (teacherId) {
            $.ajax({
                url: "/queryTeacherByID",
                method: "GET",
                data: { tId:teacherId },
                success: function (data) {
                    // 将查询结果填充到表单中
                    $("#teacherid").val(data.teacherId);
                    $("#name").val(data.name);
                    $("#gender").val(data.gender);
                    $("#phone").val(data.phone);
                    $("#address").val(data.address);
                    $("#partymember").prop("checked", data.partymember);
                    $("#position").val(data.position);
                    $("#departmentid").val(data.departmentid);
                },
                error: function () {
                    alert("获取教师信息失败，请检查服务端是否启动！");
                }
            });
        }

        // 保存修改
        $("#saveBtn").click(function () {
            const teacherData = {
                teacherId: $("#teacherid").val(),
                name: $("#name").val(),
                gender: $("#gender").val(),
                phone: $("#phone").val(),
                address: $("#address").val(),
                partymember: $("#partymember").is(":checked"),
                position: $("#position").val(),
                departmentid: $("#departmentid").val()
            };

            $.ajax({
                url: "/updateTeacher",
                method: "POST",
                contentType: "application/json",
                data: JSON.stringify(teacherData),
                success: function () {
                    alert("教师信息修改成功！");
                    window.location.href = "teacher.html";
                },
                error: function () {
                    alert("教师信息修改失败，请重试！");
                }
            });
        });
    });
</script>
</body>
</html>
